package com.ygsoft.tianlu_mall.service.impl;

import com.ygsoft.tianlu_mall.mapper.TraceMapper;
import com.ygsoft.tianlu_mall.service.TraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class TraceServiceImpl implements TraceService {

    @Autowired
    TraceMapper traceMapper;

    @Override
    @Transactional
    public void addGoods2Trace(int user_id, int goods_id) {
        HashMap<String,Object> map = new HashMap<>();
        HashMap<String,Object> checkmap = new HashMap<>();
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        checkmap.put("goods_id",goods_id);
        checkmap.put("time",date.format(formatter));
        List<HashMap<String, Object>> checkList = traceMapper.checkGoodsInTrace(checkmap);
        map.put("trace_userid",user_id);
        map.put("trace_goodsid",goods_id);
        map.put("trace_time",date.format(formatter));
        if (checkList.isEmpty()) {
            traceMapper.addGoods2Trace(map);
        }
    }

    @Override
    public List<HashMap<String, Object>> getTrace(int user_id) {
        return traceMapper.getTrace(user_id);
    }

    @Override
    public void deleteOneTrace(int user_id, int trace_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("trace_userid",user_id);
        map.put("trace_id",trace_id);
        traceMapper.deleteOneTrace(map);
    }

    @Override
    public void deleteTraceByTime(int user_id, Date trace_time) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("trace_userid",user_id);
        map.put("trace_time",trace_time);
        traceMapper.deleteTraceByTime(map);
    }
}
